import ast
import pandas as pd

"""
计算时间转化率

先删除全天封闭的手艺人
"""
df_time = pd.read_csv('times.csv')


def order_numbers(se):
    new_genes = ast.literal_eval(se)[1:]
    order_number = len(set([g for g in new_genes if g > 0]))
    return order_number


df_time['order_number'] = df_time['genes'].apply(order_numbers)
# 去除没有订单的手艺人
df_time = df_time[df_time['order_number'] > 0]
print(df_time.shape)


def remove_closed_time(se_time):
    """
    排除关闭时间
    :param se_time:
    :return:
    """
    new_time = []
    new_disp = []
    new_order = []
    new_travel_hours = 0
    time_id = ast.literal_eval(se_time['old_inventory'])
    disp_id = ast.literal_eval(se_time['inventory'])
    order_id = ast.literal_eval(se_time['genes'])[1:]
    for i, t in enumerate(time_id):
        if t != 2:
            new_time.append(t)
            new_disp.append(disp_id[i])
            new_order.append(order_id[i])
            if order_id[i] == 0 and disp_id[i] == 1:
                new_travel_hours += 1
    orders = set([o for o in new_order if o > 0])
    old_time_usage = sum(new_time)
    new_time_usage = sum(new_disp)
    return len(orders), old_time_usage, new_time_usage, new_travel_hours


orders_number, old_times, new_times, travel_hours = zip(*df_time.apply(remove_closed_time, axis=1))
total_orders_number = sum(list(orders_number))
total_old_times = sum(list(old_times))
total_new_times = sum(list(new_times))
artisan_number = len(df_time)
print('手艺人数量: ' + str(artisan_number))
print('订单总数: ' + str(total_orders_number))

print('使用时间: ' + str(total_old_times))
print('新使用时间: ' + str(total_new_times))

new_travel_time = sum(travel_hours)
service_time = total_new_times - new_travel_time
old_travel_time = total_old_times - service_time
print('路程时间: ' + str(old_travel_time))
print('新路程时间: ' + str(new_travel_time))
print('手艺人平均路程: ' + str(old_travel_time/artisan_number))
print('新手艺人平均路程: ' + str(new_travel_time/artisan_number))
print('手艺人平均节省时间: ' + str((total_old_times - total_new_times)/artisan_number))
old_travel_percent = old_travel_time/total_old_times
print('路程时间占比: ' + str(old_travel_percent))
new_travel_percent = new_travel_time/total_new_times
print('新路程时间占比: ' + str(new_travel_percent))

print('时间节约率: ' + str((total_old_times - total_new_times)/total_old_times))
save_travel_time_rate = (old_travel_time - new_travel_time) / old_travel_time
print('路程时间节约率: ' + str(save_travel_time_rate))

inventory_usage = total_orders_number / total_old_times
print('时间利用率: ' + str(inventory_usage))
new_inventory_usage = total_orders_number / total_new_times
print('新时间利用率: ' + str(new_inventory_usage))

travel_time_percent_rate = (old_travel_percent - new_travel_percent)/old_travel_percent
print('路程时间占比率减少: ' + str(travel_time_percent_rate))
print('时间利用率增长: ' + str(new_inventory_usage/inventory_usage))

# 总订单数: 303
# 使用时间: 1793
# 时间利用率: 0.1689905186837702
# 新使用时间: 619
# 新时间利用率: 0.4894991922455573
# 预期增长幅度: 2.8966074313408723


# 总订单数: 184
# 使用时间: 950
# 时间利用率: 0.1936842105263158
# 新使用时间: 541
# 新时间利用率: 0.34011090573012936
# 预期增长幅度: 1.7560073937153418


# 总订单数: 184
# 使用时间: 654
# 时间利用率: 0.28134556574923547
# 新使用时间: 541
# 新时间利用率: 0.34011090573012936
# 预期增长幅度: 1.2088724584103512
